#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <math.h>
struct Tree
{
    int row;
    int col;
    int peeNumber;
};
int main()
{
    struct Tree tree[400] = { 0 };
    int index = 0;
    int i, j;
    int m, n, temp, time;
    scanf("%d %d %d", &m, &n, &time);
    for (i = 1; i <= m; i++)
    {
        for (j = 1; j <= n; j++)
        {
            scanf("%d", &temp);
            if (temp != 0)
            {
                    int k = 0;
                    for (k = 0; k < index; k++)
                    {
                        if (tree[k].peeNumber < temp)
                        {
                            break;
                        }
                    }
                    if (k == index)
                    {
                        tree[index].row = i;
                        tree[index].col = j;
                        tree[index].peeNumber = temp;
                        index++;
                    }
                    else
                    {
                        int n = k;
                        for (n = index+1; n > k; n--)
                        {
                            tree[n] = tree[n-1];
                        }
                        tree[k].row = i;
                        tree[k].col = j;
                        tree[k].peeNumber = temp;
                        index++;
                    }
            }
        }
    }
    int flag = 1, timetemp = 0, maxsum = 0, tempmax = 0;
    for (i = 0; i < index; i++)
    {
        if (flag == 1)
        {
            timetemp += tree[i].row;
            flag = 0;
        }
        else
        {
            timetemp += (abs(tree[i].row - tree[i - 1].row) + abs(tree[i].col - tree[i-1].col));
        }
        tempmax += tree[i].peeNumber;
        timetemp += 1;
        if ((timetemp + tree[i].row) > time)
            break;
        else
        {
            maxsum = tempmax;
        }
    }
    printf("%d", maxsum);
    return 0;
}